package com.oscar.ehome.view.konwledge;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.knowpromblebean.KnowlePrombleBean;
import com.oscar.ehome.bean.pagebean.ClassRoomPrombleBean;
import com.oscar.ehome.presenter.KnowelgePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.konwledge.view.KnowprombleView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.ClassRoomPrombleAdapter;

import org.apache.commons.lang.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/21
 * 回答问题页
 */

public class AnswerPrombleActivity extends BasesActivity<KnowprombleView, KnowelgePresenter> implements KnowprombleView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * 问题
     */
    @BindView(R.id.answerpromble_text)
    TextView answerprombleText;
    /**
     * 回答
     */
    @BindView(R.id.answer_edtext)
    EditText answerEdtext;
    /**
     * 提交
     */
    @BindView(R.id.send_promble_buton)
    Button sendPrombleButon;

    KnowelgePresenter presenter;
    String promble_text = "";
    private String token = "";
    String questionId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answerpromble);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected KnowelgePresenter createPresenter() {
        presenter = new KnowelgePresenter(this);
        return presenter;
    }

    public void initView() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(AnswerPrombleActivity.this).getSystemConfig().getColor()));
        token = SystemConfigFactory.getInstance(AnswerPrombleActivity.this).getSystemConfig().getToken();
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleTopbar.setText("问题");

        questionId = getIntent().getStringExtra("questionId");
        promble_text = getIntent().getStringExtra("promble_text");
        answerprombleText.setText(promble_text);
    }

    @OnClick({R.id.title_layout_left, R.id.send_promble_buton})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left://返回
                finish();
                break;
            case R.id.send_promble_buton://提交问题
                if (StringUtils.isNotEmpty(answerEdtext.getText().toString())) {
                    presenter.answerPrombleList(token, questionId, answerEdtext.getText().toString());
                    showLoading();
                } else {
                    ToastUtil.show(AnswerPrombleActivity.this, "请先回答问题.");
                }
                break;
        }
    }

    @Override
    public void answerPromble(String bean) {
        dismissLoading();
        ToastUtil.show(AnswerPrombleActivity.this, bean);
        finish();
    }

    @Override
    public void getKnowPrombles(ClassRoomPrombleBean result) {

    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(AnswerPrombleActivity.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(AnswerPrombleActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(AnswerPrombleActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(AnswerPrombleActivity.this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(AnswerPrombleActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
